package com.sigmazero13.MKDummy.components;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import com.sigmazero13.MKDummy.R;
import com.sigmazero13.MKDummy.models.CardDeck;
import com.sigmazero13.MKDummy.models.ManaColor;


public class CardDisplayIconCountView extends CardDisplayView {
	
	CardDeck deck;
	Bitmap redImg;
	Bitmap blueImg;
	Bitmap greenImg;
	Bitmap whiteImg;
	Bitmap woundImg;
	Bitmap spellImg;
	View drawView;

	public CardDisplayIconCountView(Context c) {
		super(c);
		
		commonSetup(c);
	}

	public CardDisplayIconCountView(Context c, AttributeSet attrs) {
		super(c, attrs);
		
		commonSetup(c);
	}
	
	@Override
	public void onDeckChanged(CardDeck deck) {
		this.deck = deck;
		this.invalidate();
		drawView.invalidate();
	}	
	
	private void commonSetup(Context c) {
		LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.player_data_draw, this, true);
		
		redImg = BitmapFactory.decodeResource(getResources(), R.drawable.card_red);
		blueImg = BitmapFactory.decodeResource(getResources(), R.drawable.card_blue);
		greenImg = BitmapFactory.decodeResource(getResources(), R.drawable.card_green);
		whiteImg = BitmapFactory.decodeResource(getResources(), R.drawable.card_white);
		woundImg = BitmapFactory.decodeResource(getResources(), R.drawable.card_wound);
		spellImg = BitmapFactory.decodeResource(getResources(), R.drawable.card_spell_overlay);
		
		drawView = new DrawView(c);
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		drawView.setLayoutParams(lp);
		this.addView(drawView);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int w = MeasureSpec.getSize(widthMeasureSpec);
		int h = redImg.getHeight() + 10;
		drawView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(h, MeasureSpec.UNSPECIFIED));
		setMeasuredDimension(w,h);
	}
	
	
	private class DrawView extends View {
		private List<CardInfo> cardOrder;
		
		public DrawView(Context c) {
			super(c);
			cardOrder = new ArrayList<CardInfo>();
			
			cardOrder.add(new CardInfo(ManaColor.RED, redImg));
			cardOrder.add(new CardInfo(ManaColor.BLUE, blueImg));
			cardOrder.add(new CardInfo(ManaColor.GREEN, greenImg));
			cardOrder.add(new CardInfo(ManaColor.WHITE, whiteImg));
			cardOrder.add(new CardInfo(ManaColor.WOUND, woundImg));
		}
		
		@Override
		public void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			
			if (deck.countCardsRemaining() == 0) return;
			
			int cardWidth = redImg.getWidth();
			float cardSpacing = cardWidth/3;
			float stackSpacing = cardWidth;
			
			float totalRoom = 0;
			int numStacks = 0;
			for (CardInfo ci : cardOrder) {
				float stackWidth = getStackWidth(deck.countCardsRemainingOfColor(ci.getColor()), cardWidth, cardSpacing);
				if (stackWidth > 0) {
					totalRoom += stackWidth;
					numStacks += 1;
				}
			}
			if (numStacks > 0) {
				totalRoom += (numStacks-1)*(cardSpacing+stackSpacing);
			}
			
			if (totalRoom > canvas.getWidth()) {
				float leftover = canvas.getWidth() - (cardWidth + (deck.countCardsRemaining()-1)*cardSpacing);
				if (leftover > 0) {
					stackSpacing = leftover/numStacks;
					if (stackSpacing > cardWidth) stackSpacing = cardWidth;
				} else {
					leftover = canvas.getWidth() - cardWidth;
					cardSpacing = leftover/(deck.countCardsRemaining()-1);
					stackSpacing = 0;
				}
			}
			
			float x = 0;
			for (CardInfo ci : cardOrder) {
				int remaining = deck.countCardsRemainingOfColor(ci.getColor());
				if (remaining == 0) continue;
				if (x > 0) {
					x += stackSpacing;
				}
				
				for (int i = 0; i < remaining; i++) {
					canvas.drawBitmap(ci.getCard(), x, 5, null);
					if (i == remaining-1 && deck.hasSpellOfColor(ci.getColor())) {
						canvas.drawBitmap(spellImg, x, 5, null);
					}
					x += cardSpacing;
				}
			}
		}
		
		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			int w = MeasureSpec.getSize(widthMeasureSpec);
			int h = MeasureSpec.getSize(heightMeasureSpec);
			setMeasuredDimension(w,h);
		}	
		
		private float getStackWidth(int numCards, int cardWidth, float spacing) {
			if (numCards == 0) {
				return 0;
			} else {
				return cardWidth + (numCards-1)*spacing;
			}
		}
	}
	
	private class CardInfo {
		private ManaColor color;
		private Bitmap card;
		
		public CardInfo(ManaColor color, Bitmap card) {
			this.color = color;
			this.card = card;
		}
		
		public ManaColor getColor() {
			return color;
		}
		
		public Bitmap getCard() {
			return card;
		}
	}

}
